package com.zimmur.tools;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class HtmlToTextUtil {
	private static Logger log=LoggerFactory.getLogger(HtmlToTextUtil.class);
	public static String htmlToText(String html){
		try {
			Pattern r = Pattern.compile("<(S*?)[^>]*>.*?|<.*? />");
			Matcher m = r.matcher(html);
			if(m.find()){
				html=html.replace("\t", "").replace("\r\n", "").replaceAll("<[Bb][Rr][^>]+>", "\r\n")
						.replaceAll("<[^>]+>", "");
			}
			html=html.replace("&nbsp;"," ");
		} catch (Exception e) {
			log.error(e.getMessage());
			log.error("reason : " +html);
			return "";
		}
		return html;
	}
	public static String TextToHtml(String text){
		try {
			Pattern r = Pattern.compile("<(S*?)[^>]*>.*?|<.*? />");
			Matcher m = r.matcher(text);
			if(!m.find()){
				text=text.replace("\r\n", "<br/>").replace(" ", "&nbsp;").replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			log.error("reason : " +text);
			return "";
		}
		return text;
	}
}
